package com.ruoyi.project.park.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.park.domain.BizParkingCard;
import com.ruoyi.project.park.service.IBizParkingCardService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 停车会员卡管理Controller
 * 
 * @author ruoyi
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/park/card")
public class BizParkingCardController extends BaseController
{
    @Autowired
    private IBizParkingCardService bizParkingCardService;

    /**
     * 查询停车会员卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('park:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizParkingCard bizParkingCard)
    {
        //判断当前登录用户是否为管理员
        if(!getLoginUser().getUser().getUserId().equals(1L)){
          //不是管理员
            bizParkingCard.setCardNumber(getUsername());
        }


        startPage();
        List<BizParkingCard> list = bizParkingCardService.selectBizParkingCardList(bizParkingCard);
        return getDataTable(list);
    }

    /**
     * 导出停车会员卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('park:card:export')")
    @Log(title = "停车会员卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizParkingCard bizParkingCard)
    {
        List<BizParkingCard> list = bizParkingCardService.selectBizParkingCardList(bizParkingCard);
        ExcelUtil<BizParkingCard> util = new ExcelUtil<BizParkingCard>(BizParkingCard.class);
        util.exportExcel(response, list, "停车会员卡管理数据");
    }

    /**
     * 获取停车会员卡管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('park:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(bizParkingCardService.selectBizParkingCardById(id));
    }

    /**
     * 新增停车会员卡管理
     */
    @PreAuthorize("@ss.hasPermi('park:card:add')")
    @Log(title = "停车会员卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizParkingCard bizParkingCard)
    {
        //生成雪花id
        String nextIdStr = new Snowflake(1, 1).nextIdStr();
        bizParkingCard.setId(nextIdStr);
        return toAjax(bizParkingCardService.insertBizParkingCard(bizParkingCard));
    }

    /**
     * 修改停车会员卡管理
     */
    @PreAuthorize("@ss.hasPermi('park:card:edit')")
    @Log(title = "停车会员卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizParkingCard bizParkingCard)
    {
        return toAjax(bizParkingCardService.updateBizParkingCard(bizParkingCard));
    }

    /**
     * 删除停车会员卡管理
     */
    @PreAuthorize("@ss.hasPermi('park:card:remove')")
    @Log(title = "停车会员卡管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bizParkingCardService.deleteBizParkingCardByIds(ids));
    }

    public static void main(String[] args) {
        //hutools 时间戳转Date
        // 假设有一个长整型的时间戳（单位：毫秒）
        long timestampInMilliseconds = 1711247505L *1000;

        // 直接将毫秒时间戳转换为Date对象
        Date date = DateUtil.date(timestampInMilliseconds);

        System.out.println(date);
    }
}
